__author__ = 'Muscle'


from mui import *
from mui.Widgets import *
import time
import mui.color as color

class DragAbleComp(MUIComponent):
    def __init__(self, parent=None,  **kwargs):
        MUIComponent.__init__(self, parent, **kwargs)

    def onQueryOleDropData(self):
        return ["c:\\test.txt"]

    def onQueryMUIDropData(self):
        return ["c:\\test2.txt"]

    def onPrepareStartDrag(self, x, y):
        #print '[component.py] onPrepareStartDrag'
        pnt = self.local2global(x, y)
        _thumb = MUIImage(self.window, left = 0, top = 0, width = 30, height = 30, image = "testImg.jpg", bgColor = color.blue, imageOpacity = 0.5, opacity = 0.0)
        self.window.prepareDragDrop(pnt[0], pnt[1], self, _thumb)

class DropAbleComp(MUIComponent):
    def __init__(self, parent=None,  **kwargs):
        MUIComponent.__init__(self, parent, **kwargs)
        self.bind('Drop Files', self._onDropFiles)

    def _onDropFiles(self,  x, y, files, muiClipInfo):
        print '_onDropFiles:', x, y, files, muiClipInfo


class MyWindow(MUIWindow):
    def __init__(self, **kwargs):
        MUIWindow.__init__(self, **kwargs)
        self.background = 'dialog\\volume_bg.png |(10,10,10,10)'

        self.comp1 = DropAbleComp(self, left = 50, top = 50, width = 200, height = 200, bgColor = color.red)
        self.comp1.canAcceptDropFiles = True
        self.comp2 = DragAbleComp(self, left = 50, top = 350, width = 200, height = 200, bgColor = color.green)
        self.comp2.canDragFiles = True
        # for FPS
        self.__fpsCount = 0
        self.__fpsTimeStart = 0;
        self.__fosTimeEnd = 0;
        self.fpsText = MUIText(self, right = self.width-20, top = 20, width = 200, height = 40, text = "FPS: 0", fontSize = 20, fontColor = color.blue, fontWeight = 'Bold')
        self.bindData('width', self.fpsText, 'right', dir = '->', converter =  lambda x: x-20 )


    def on_paint(self):
        if (self.__fpsCount == 0):
            self.__fpsTimeStart = time.clock()
        super(MyWindow, self).on_paint()

        self.__fpsCount += 1

        if (self.__fpsCount >= 120):
            self.__fpsTimeEnd = time.clock()
            diffTime = self.__fpsTimeEnd - self.__fpsTimeStart
            fps = self.__fpsCount / diffTime
            self.fpsText.text = "FPS: " + str(int(fps))
            self.__fpsCount = 0



def main():
    mui.muiInit(main)
    g_DXDevice = muiDX.CDXDevice()
    w = MyWindow(width = 1024, height = 768, dxDevice = g_DXDevice)
    w.ShowWindow(1)
    muiApp.run()
    g_DXDevice.Release()
    mui.muiUnInit()



main()
